#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#include"ObjectWriter.h"
using namespace std;

ObjectWriter::ObjectWriter(const char* file){
	out = fopen(file, "wb");
}

ObjectWriter::~ObjectWriter(){
	fclose(out);
}

void ObjectWriter::writeInt(int n){
	fwrite(&n, sizeof(int), 1, out);
}

void ObjectWriter::writeBool(bool b){
	fwrite(&b, sizeof(bool), 1, out);
}

void ObjectWriter::writeVector(std::vector<string*> &v){
	int len = (int)(v.size());
	fwrite(&len, sizeof(int), 1, out);
	for(int i = 0; i < len; i++){
		writeString(v[i]);
	}
}

void ObjectWriter::writeVector(std::vector<int> &v){
	int len = (int)(v.size());
	writeInt(len);
	for(int i = 0; i < len; i++){
		writeInt(v[i]);
	}
}

void ObjectWriter::writeVector(std::vector<vector<string*>*> &v){
	int len = (int)(v.size());
	writeInt(len);
	for(int i = 0; i < len; i++){
		writeVector(*v[i]);
	}
}

void ObjectWriter::writeMap(HashMap &Map){
	HashMap::iterator iter;
	int size = (int)(Map.size());
	writeInt(size);
	for(iter = Map.begin();iter != Map.end();iter++){
		writeString(iter->first);
		writeInt(iter->second);
	}
}

void ObjectWriter::writeString(std::string* str){
	int length = (int)(str->length());
	fwrite(&length, sizeof(int), 1, out);
	fwrite(str->c_str(), sizeof(char), length + 1, out);
}



